* Load metadata from the file itself
*/
function loadFromFile() {
- global $wgUseSharedUploads, $wgSharedUploadDirectory, $wgContLang,
- $wgShowEXIF;
+ global $wgUseSharedUploads, $wgSharedUploadDirectory, $wgContLang, $wgShowEXIF;
$fname = 'Image::loadFromFile';
wfProfileIn( $fname );
$this->imagePath = $this->getFullPath();
$base = $wgUploadBaseUrl;
$path = $wgUploadPath;
}
- $url = "{$base}{$path}" . wfGetHashPath($name, $fromSharedDirectory) . "{$name}";
- return wfUrlencode( $url );
+ $url = "{$base}{$path}" . wfGetHashPath($name, $fromSharedDirectory) . "{$name}";
+ return wfUrlencode( $url );
}
/**
if ($this->canRender()) {
if ( $width > $this->width * $height / $this->height )
- $width = floor( $this->width * $height / $this->height );
- # Note this is the largest width such that the thumbnail's
- # height is at most $height.
-
+ $width = wfFitBoxWidth( $this->width, $this->height, $height );
$thumb = $this->renderThumb( $width );
}
else $thumb= NULL; #not a bitmap or renderable image, don't try.
*/
function renderThumb( $width, $useScript = true ) {
global $wgUseSquid, $wgInternalServer;
- global $wgThumbnailScriptPath, $wgSharedThumbnailScriptPath;
global $wgSVGMaxSize, $wgMaxImageArea, $wgThumbnailEpoch;
$fname = 'Image::renderThumb';
* @access private
*/
function reallyRenderThumb( $thumbPath, $width, $height ) {
- global $wgSVGConverters, $wgSVGConverter,
- $wgUseImageMagick, $wgImageMagickConvertCommand;
+ global $wgSVGConverters, $wgSVGConverter;
+ global $wgUseImageMagick, $wgImageMagickConvertCommand;
+ global $wgCustomConvertCommand;
$this->load();
$cmd = wfEscapeShellArg($wgImageMagickConvertCommand) .
" {$quality} -background white -size {$width} ".
- wfEscapeShellArg($this->imagePath) . " -resize {$width}x{$height} -depth 8 " .
+ wfEscapeShellArg($this->imagePath) .
+ // For the -resize option a "!" is needed to force exact size,
+ // or ImageMagick may decide your ratio is wrong and slice off
+ // a pixel.
+ " -resize " . wfEscapeShellArg( "{$width}x{$height}!" ) .
+ " -depth 8 " .
wfEscapeShellArg($thumbPath) . " 2>&1";
wfDebug("reallyRenderThumb: running ImageMagick: $cmd\n");
wfProfileIn( 'convert' );
$err = wfShellExec( $cmd );
wfProfileOut( 'convert' );
+ } elseif( $wgCustomConvertCommand ) {
+ # Use a custom convert command
+ # Variables: %s %d %w %h
+ $src = wfEscapeShellArg( $this->imagePath );
+ $dst = wfEscapeShellArg( $thumbPath );
+ $cmd = $wgCustomConvertCommand;
+ $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
+ $cmd = str_replace( '%h', $height, str_replace( '%w', $width, $cmd ) ); # Size
+ wfDebug( "reallyRenderThumb: Running custom convert command $cmd\n" );
+ wfProfileIn( 'convert' );
+ $err = wfShellExec( $cmd );
+ wfProfileOut( 'convert' );
} else {
# Use PHP's builtin GD library functions.
#
}
function checkDBSchema(&$db) {
- global $wgCheckDBSchema;
+ global $wgCheckDBSchema;
if (!$wgCheckDBSchema) {
return;
}
* @access public
*/
function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) {
- global $wgUploadDirectory, $wgHashedUploadDirectory,
- $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory;
+ global $wgUploadDirectory, $wgHashedUploadDirectory;
+ global $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory;
$dir = $shared ? $wgSharedUploadDirectory : $wgUploadDirectory;
$hashdir = $shared ? $wgHashedSharedUploadDirectory : $wgHashedUploadDirectory;
if (!$hashdir) { return $dir.'/'.$subdir; }
}
}
+
+/**
+ * Calculate the largest thumbnail width for a given original file size
+ * such that the thumbnail's height is at most $maxHeight.
+ * @param int $boxWidth
+ * @param int $boxHeight
+ * @param int $maxHeight
+ * @return int
+ */
+function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
+ $idealWidth = $boxWidth * $maxHeight / $boxHeight;
+ $roundedUp = ceil( $idealWidth );
+ if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
+ return floor( $idealWidth );
+ else
+ return $roundedUp;
+}
+
?>